﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Web;

namespace PlexBB
{
    internal class Util
    {
        public static Stream GetResourceStream(string resourceLocalPath)
        {
            // secure resource local path
            resourceLocalPath = resourceLocalPath.Replace('\\', '/').TrimStart('/');
#if PLEXBB_PRECOMPILED
            string resourceName = "PlexBB." + resourceLocalPath.Replace('/', '.');
            Assembly assembly = Assembly.GetExecutingAssembly();
            return assembly.GetManifestResourceStream(resourceName);
#else
            string physicalPath =  HttpContext.Current.Server.MapPath("~/App_Code/PlexBB/" + resourceLocalPath);
            return File.Open(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read);
#endif
        }

        public static bool CompareByteArrays(byte[] data1, byte[] data2)
        {
            // If both are null, they're equal
            if (data1 == null && data2 == null)
            {
                return true;
            }
            // If either but not both are null, they're not equal
            if (data1 == null || data2 == null)
            {
                return false;
            }
            if (data1.Length != data2.Length)
            {
                return false;
            }
            for (int i = 0; i < data1.Length; i++)
            {
                if (data1[i] != data2[i])
                {
                    return false;
                }
            }
            return true;
        }

        //public static string ConvertToMaxBase(int i)
        //{
        //    if (i < 0)
        //    {
        //        throw new ArgumentOutOfRangeException("i");
        //    }
        //    const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        //    StringBuilder result = new StringBuilder();
        //    do
        //    {
        //        result.Insert(0, chars[i % chars.Length]);
        //        i /= chars.Length;
        //    } while (i > 0);
        //    return result.ToString();
        //}
    }
}
